/*
 * @Author: your name
 * @Date: 2022-01-22 15:03:35
 * @LastEditTime: 2022-01-29 16:07:21
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \my-app\src\utils\request.ts
 */
import axios from 'axios'

const request = axios.create({
	baseURL: '',
	timeout: 10000,
})

request.interceptors.request.use(config => {
	return config
})

request.interceptors.response.use(
	res => {
		return res
	},
	err => {
		return Promise.reject(new Error(err))
	}
)
interface Params {
	[T: string]: any
}

type Optional = Params | null | '' | undefined
export const get = (url: string, params?: Params, config?: any) => {
	const paramsConfig: Params = {
		url,
		method: 'GET',
		params,
	}
	if (config) paramsConfig.responseType = config.responseType
	return request(paramsConfig)
}

export const post = (url: string, data?: Optional, params?: Params, config?: any) => {
	const paramsConfig: Params = {
		url,
		method: 'POST',
		params,
		data,
	}
	if (config) paramsConfig.responseType = config.responseType
	return request(paramsConfig)
}
